#include <iostream>

using namespace std;

const int N = 5e4 + 10;
int fa[N * 3];
int n, k;

int find(int x)
{
	return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void un(int x, int y)
{
	int dx = find(x);
	int dy = find(y);
	fa[dy] = dx;
}
bool issame(int x, int y)
{
	return find(x) == find(y);
}


int main()
{
	cin >> n >> k;
	for(int i = 1; i <= n * 3; i++) fa[i] = i;
	int ret = 0;
	while(k--)
	{
		int op, x, y; cin >> op >> y >> x;
		if(x > n || y > n) 
		{
			ret++;
			continue;
		}
		if(op == 1)
		{
			if(issame(x, y + n) || issame(x, y + 2 * n)) 
			{
				ret++;
				continue;
			}
			un(x, y);
			un(x + n, y + n);
			un(x + 2 * n, y + 2 * n);
		}
		else
		{
			if(issame(x, y) || issame(x, y + n))
			{
				ret++;
				continue;
			}
			un(x, y + 2 * n);
			un(y, x + n);
			un(x + 2 * n, y + n);
		}
	}
	
	cout << ret << endl;
	
	return 0;
}
